//
//  CFRMAddRecipeController.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit

import ProgressHUD
class CFRMAddRecipeController: CFRMBaseViewController {
    @IBOutlet var breakfastTV: UITextView!
    @IBOutlet var dinnerTV: UITextView!
    var model: CFRMRecipeModel?  = nil
    @IBOutlet var lunchTV: UITextView!
    var isEdit = false
    @IBOutlet var nameTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Recipe addition"
        self.addRightBarButton(imageName: "icon_save")
        self.breakfastTV.placeholder = "please enter breakfast"
        self.breakfastTV.placeholderColor = .lightGray
        
        self.lunchTV.placeholder = "please enter lunch"
        self.lunchTV.placeholderColor = .lightGray
        
        self.dinnerTV.placeholder = "please enter dinner"
        self.dinnerTV.placeholderColor = .lightGray
        if self.isEdit {
            setEditModel()
        }
        // Do any additional setup after loading the view.
    }
    func setEditModel(){
        self.nameTF.text = self.model?.name
        self.breakfastTV.text = self.model?.breakfast
        self.lunchTV.text = self.model?.lunch
        self.dinnerTV.text = self.model?.dinner
    }
    override func clockRightBarButtonAction() {
        self.view.endEditing(true)
        if self.nameTF.text!.isEmpty {
            showTips("Please enter recipe name")
            return
        }
        if self.breakfastTV.text!.isEmpty {
            showTips("Please enter breakfast")
            return
        }
        if self.lunchTV.text!.isEmpty {
            showTips("Please enter lunch")
            return
        }
        if self.dinnerTV.text!.isEmpty {
            showTips("Please enter dinner")
            return
        }
        
        
        if !self.isEdit {
            self.model = CFRMRecipeModel()
        }
        model?.name = self.nameTF.text!
        model?.breakfast = self.breakfastTV.text!
        model?.lunch = self.lunchTV.text!
        model?.dinner = self.dinnerTV.text!
        if !self.isEdit{
            model?.isDefault = false
            model?.saveToDB()
            let helper = CFRMRecipeModel.getUsingLKDBHelper()
            helper.delete(with: CFRMRecipeModel.self, where: "isDefault = true")
            
            let helper1 = CFRMRecipeHistoryModel.getUsingLKDBHelper()
            helper1.delete(with: CFRMRecipeHistoryModel.self, where: "isDefault = true")
        }else{
            model?.updateToDB()
        }
        ProgressHUD.showSuccess(self.isEdit ? "Recipe updated succeed" : "Recipe addition succeed")
        
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    
    func showTips(_ message:String){
        ProgressHUD.showError(message)
    }
   
}
